package com.tianhui.consignortwofei.mvp.ui.activity.hetong.activity;

import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;

import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.hetong.BankAccountInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.CompanyInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.SealUnitInfo;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/5/19
 * @description
 **/
public class ContractAddStepTwoActivity extends BaseContractAddActivity {


    @BindView(R.id.activity_contract_add_step_two_sealUnitClickItemView)
    protected ClickItemView mSealUnitClickItemView;
    @BindView(R.id.activity_contract_add_step_two_accountClickItemView)
    protected ClickItemView mAccountClickItemView;
    @BindView(R.id.activity_contract_add_step_two_bankNameInputItemView)
    protected InputItemView mBankNameInputItemView;
    @BindView(R.id.activity_contract_add_step_two_signingTimeClickItemView)
    protected ClickItemView mSigningTimeClickItemView;
    @BindView(R.id.activity_contract_add_step_two_addressInputItemView)
    protected InputItemView mSigningAddressInputItemView;


    private SelectDialog mSealUnitSelectDialog;
    private SelectDialog mBankAccountSelectDialog;
    private TimePickerView mSigningTimePickerView;

    private List<CompanyInfo> companyFirstPartyInfoList;
    private List<SealUnitInfo> mSealUnitInfoList;

    private boolean isRefreshBankAccount;
    private String sealUnitCompanyName;
    private String bankAccount;

    @Override
    public int getLayoutId() {
        return R.layout.activity_contract_add_step_two;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public ContractInfo getSaveInfo() {
        String sealUnit = mSealUnitClickItemView.getContent();
        if (TextUtils.isEmpty(sealUnit)) {
            showToast("请选择盖章单位");
            return null;
        }
        mContractInfo.company_a = sealUnitCompanyName;
        mContractInfo.accountname = sealUnitCompanyName;

        String account = mAccountClickItemView.getContent();
        if (TextUtils.isEmpty(account)) {
            showToast("请选择账号");
            return null;
        }
        mContractInfo.banknumber = bankAccount;
        String bankName = mBankNameInputItemView.getContent();
        if (TextUtils.isEmpty(bankName)) {
            showToast("请输入开户行");
            return null;
        }
        mContractInfo.bankopen = bankName;


        String singingDate = mSigningTimeClickItemView.getContent();
        if (TextUtils.isEmpty(singingDate)) {
            showToast("请选择签订时间");
            return null;
        }

        mContractInfo.signedate = singingDate;
        String singingAddress = mSigningAddressInputItemView.getContent();
        mContractInfo.signedat = singingAddress;


        return mContractInfo;
    }

    @Override
    public void initData() {
        sealUnitCompanyName = mContractInfo.company_a;
        bankAccount = mContractInfo.banknumber;
        requestSealUnitList(false);
        requestCompanyFirstPartyList();
    }

    @Override
    public void initView() {

        String singingDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");
        mSigningTimeClickItemView.setContent(singingDate);

    }

    @Override
    public void initListener() {

    }

    private void requestCompanyFirstPartyList() {
        mContractModel.companyFirstPartyListModel(getContext(), true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<CompanyInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<CompanyInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<CompanyInfo> companyInfoList = commonResponse.getData();
                    if (companyInfoList != null && companyInfoList.size() > 0) {
                        companyFirstPartyInfoList = companyInfoList;
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    private void showSealUnitDialog(List<SealUnitInfo> sealUnitInfoList) {

        mSealUnitSelectDialog = new SelectDialog<SealUnitInfo>(this);
        mSealUnitSelectDialog.setTitle("盖章单位");
        mSealUnitSelectDialog.setList(sealUnitInfoList);
        mSealUnitSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<SealUnitInfo>() {
            @Override
            public void onItemClick(SealUnitInfo sealUnitInfo, int position) {
                mSealUnitClickItemView.setContent(sealUnitInfo.getSelectItemText());
                mContractInfo.seal = sealUnitInfo.getSelectItemTextValue();
                if (!TextUtils.isEmpty(sealUnitCompanyName)) {
                    if (sealUnitCompanyName.equals(sealUnitInfo.contract)) {
                        isRefreshBankAccount = false;
                    } else {
                        isRefreshBankAccount = true;
                        clearBankAccountInfo();
                    }
                }
                sealUnitCompanyName = sealUnitInfo.contract;

                CompanyInfo companyInfo = getCompanyFirstPartyInfo(sealUnitInfo.contract);
                if (companyInfo != null) {
                    mContractInfo.legalperson_a = companyInfo.legalperson;
                    mContractInfo.address_a = companyInfo.address;
                }

            }
        });
        mSealUnitSelectDialog.show();

    }

    private CompanyInfo getCompanyFirstPartyInfo(String companyName) {
        CompanyInfo companyInfo = null;
        if (companyFirstPartyInfoList != null && companyFirstPartyInfoList.size() > 0) {
            int size = companyFirstPartyInfoList.size();
            for (int i = 0; i < size; i++) {
                companyInfo = companyFirstPartyInfoList.get(i);
                if (!TextUtils.isEmpty(companyName) && !TextUtils.isEmpty(companyInfo.name) && companyName.equals(companyInfo.name)) {
                    return companyInfo;
                }
            }
        }
        if (companyInfo == null) {
            companyInfo = new CompanyInfo();
        }
        return companyInfo;
    }

    private void clearBankAccountInfo() {
        mAccountClickItemView.setContent("");
        mBankNameInputItemView.setContent("");
        mContractInfo.banknumber = "";
        mContractInfo.bankopen = "";
    }

    private void showBankAccountDialog(List<BankAccountInfo> bankAccountInfoList) {

        mBankAccountSelectDialog = new SelectDialog<BankAccountInfo>(this);
        mBankAccountSelectDialog.setTitle("账号");
        mBankAccountSelectDialog.setList(bankAccountInfoList);
        mBankAccountSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<BankAccountInfo>() {
            @Override
            public void onItemClick(BankAccountInfo bankAccountInfo, int position) {
                mAccountClickItemView.setContent(bankAccountInfo.getSelectItemText());
                mBankNameInputItemView.setContent(bankAccountInfo.bankaddress);
                bankAccount = bankAccountInfo.banknumber;
            }
        });
        mBankAccountSelectDialog.show();


    }


    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @OnClick(R.id.activity_contract_add_step_two_sealUnitClickItemView)
    public void selectSealUnit() {
        if (mSealUnitSelectDialog == null) {
            if (mSealUnitInfoList != null && mSealUnitInfoList.size() > 0) {
                showSealUnitDialog(mSealUnitInfoList);
            } else {
                requestSealUnitList(true);
            }
        } else {
            mSealUnitSelectDialog.show();
        }
    }

    @OnClick(R.id.activity_contract_add_step_two_accountClickItemView)
    public void selectBankAccount() {
        if (mBankAccountSelectDialog == null || isRefreshBankAccount) {
            if (!TextUtils.isEmpty(sealUnitCompanyName)) {
                requestBankAccountList(sealUnitCompanyName);
            } else {
                showToast("未找到账号信息");
            }
        } else {
            mBankAccountSelectDialog.show();
        }
    }


    @OnClick(R.id.activity_contract_add_step_two_nextButton)
    public void next() {
        ContractInfo contractInfo = getSaveInfo();
        if (contractInfo != null) {
            openNextActivity(contractInfo);
        }

    }

    protected void openNextActivity(ContractInfo contractInfo) {
        openNextStepActivity(contractInfo,ContractAddStepThreeActivity.class);
    }

    @OnClick(R.id.activity_contract_add_step_two_signingTimeClickItemView)
    public void selectSigningTime() {
        hideKeyboard();
        if (mSigningTimePickerView == null) {

            mSigningTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mSigningTimeClickItemView.setContent(time);
                    mContractInfo.signedate = time;
                }
            })
                    .setType(TimePickerType.DEFAULT)
                    .setTitleText("签订时间")
                    .build();
        }
        mSigningTimePickerView.show();
    }


    private void requestBankAccountList(String company) {
        Map<String, String> map = new HashMap<>();
        map.put("company", company);

        mContractModel.bankAccountLisModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<BankAccountInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<BankAccountInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<BankAccountInfo> bankAccountInfoList = commonResponse.getData();
                    if (bankAccountInfoList != null && bankAccountInfoList.size() > 0) {
                        showBankAccountDialog(bankAccountInfoList);
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestSealUnitList(boolean isNeedShowDialog) {
        Map<String, String> map = new HashMap<>();
        mContractModel.sealUnitListModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<SealUnitInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<SealUnitInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<SealUnitInfo> sealUnitInfoList = commonResponse.getData();
                    if (sealUnitInfoList != null && sealUnitInfoList.size() > 0) {
                        mSealUnitInfoList = sealUnitInfoList;
                        initSealUnitInfo();
                        if (isNeedShowDialog)
                            showSealUnitDialog(sealUnitInfoList);
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void initSealUnitInfo() {
        if (mSealUnitInfoList != null && mSealUnitInfoList.size() > 0) {
            for (SealUnitInfo info : mSealUnitInfoList) {
                if (!TextUtils.isEmpty(mContractInfo.company_a) && info.contract.equals(mContractInfo.company_a)) {
                    mSealUnitClickItemView.setContent(info.deptName + "|" + info.contract);
                    break;
                }
            }
        }
    }
}
